<?php
if(!isset($_GET['key'])){
	$get_data['time'] = time();
	$get_data['client_code'] = 'iWCyYOxPrKrbivGy2S2NAwz9CYP1zrjL';
	$get_data['interface'] = 'drawboard';
	$get_data['action'] = 'setDbData';
	$post_data['redirect_uri'] = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
	$tmp_data = array_merge($get_data,$post_data);
	$tmp_data['client_secret'] = 'bYavGR4K517h7U14krBwhzkL7uoI9buI';
	ksort($tmp_data);
	$tmp_str = '';
	foreach($tmp_data as $k=>$v){
		if(!is_array($v)){
			$tmp_str .= $v;
		}
	}

	$auth_code = md5($tmp_str);
	$get_data['authcode'] = $auth_code;
	$get_data = http_build_query($get_data);

	$url = 'http://adapter.verystar.cn/externalapi.php?'.$get_data;
	$curl = curl_init();
	curl_setopt($curl,CURLOPT_URL,$url);
	curl_setopt($curl,CURLOPT_HEADER,0);
	curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
	if($post_data){
		curl_setopt($curl,CURLOPT_POST,1);
		$post_data = http_build_query($post_data);
		curl_setopt($curl,CURLOPT_POSTFIELDS,$post_data);
	}
	$res = curl_exec($curl);
	$state = json_decode($res,true);

	$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6173eb6085accfec&redirect_uri='.urlencode('http://crmminisite.verystar.cn/drawboard/oauth_snsapi_openid').'&response_type=code&scope=snsapi_base&state='.$state['data'].'#wechat_redirect';
	header('location: '.$url);
	exit;
}else{
	$get_data['interface'] = 'drawboard';
	$get_data['action'] = 'getDbData';
	$post_data['key'] = $_GET['key'];
	
	$get_data['time'] = time();
	$get_data['client_code'] = 'iWCyYOxPrKrbivGy2S2NAwz9CYP1zrjL';
	$tmp_data = array_merge($get_data,$post_data);
	$tmp_data['client_secret'] = 'bYavGR4K517h7U14krBwhzkL7uoI9buI';
	ksort($tmp_data);
	$tmp_str = '';
	foreach($tmp_data as $k=>$v){
		if(!is_array($v)){
			$tmp_str .= $v;
		}
	}
	
	$auth_code = md5($tmp_str);
	$get_data['authcode'] = $auth_code;
	$get_data = http_build_query($get_data);
	
	$url = 'http://adapter.verystar.cn/externalapi.php?'.$get_data;
	$curl = curl_init();
	curl_setopt($curl,CURLOPT_URL,$url);
	curl_setopt($curl,CURLOPT_HEADER,0);
	curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
	if($post_data){
		curl_setopt($curl,CURLOPT_POST,1);
		$post_data = http_build_query($post_data);
		curl_setopt($curl,CURLOPT_POSTFIELDS,$post_data);
	}
	$res = curl_exec($curl);
	$state = json_decode($res,true);
}
$openid = isset($state['data']['openid']) ? $state['data']['openid'] : -1;
$appid = 'wx6173eb6085accfec';
$self_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

$db = @mysql_connect('218.244.143.217', 'root', 'zhuoxuhui!', true);
mysql_select_db('test', $db);
$sql = 'select * from hd_code where openid = \''.$openid.'\' limit 1';
$res = mysql_query($sql, $db);
if($res){
	$row = mysql_fetch_array($res);
}
include('index.html');
?>